package java8.timeutil;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.IsoFields;

/**
 * @author Cheng Jun
 * @version 1.0
 * Description: jdk1.8 LocalDate API学习
 * 我的博客地址：https://blog.csdn.net/cauchy6317/article/details/99005855
 * @date 2019/8/9 16:29
 */
public class LocalDateStudy {

    public static void main(String[] args) {

        //获取当前日期： 2019-08-10
        LocalDate today = LocalDate.now();

        //获取年:2019
        int year = today.getYear();

        //获取月:8
        int month = today.getMonthValue();

        //获取此月的日:10
        int dayOfMonth = today.getDayOfMonth();

        //获取此年的日：223
        int dayOfYear = today.getDayOfYear();

        //获取昨天的LocalDate对象：2019-08-09
        LocalDate yesterday = today.plusDays(-1);

        //获取明天的LocalDate对象：2019-08-11
        LocalDate tomorrow = today.plusDays(1);

        //获取上一周的LocalDate对象：2019-08-03
        LocalDate preWeeks = today.plusWeeks(-1);

        //获取下一周的LocalDate对象：2019-08-17
        LocalDate nextWeeks = today.plusWeeks(1);

        System.out.println(today.isBefore(today));
        System.out.println(today.isAfter(today));

        //判断yesterday是不是在today之后：false
        boolean flag = yesterday.isAfter(today);

        //判断yesterday是不是在today之前：true
        boolean flag1 = yesterday.isBefore(today);

        //根据年月日创建LocalDate对象:2019-08-10
        LocalDate otherDay = LocalDate.of(2019, 8, 10);

        //根据文本创建LocalDate对象(JAVA诞生日):1995-05-23
        LocalDate birthday = LocalDate.parse("1995-05-23");

        //判断两个日期是否相等:true
        boolean isSameDay = otherDay.equals(today);

        //判断两个日期是否相等:false
        boolean isBirthday = birthday.equals(today);

        //更换年份并获取新的LocalDate对象：2018-08-09（注意：对象today不变）
        LocalDate preYear = today.withYear(2019);

        //在otherDay基础上修改年月日（均向后移动一个单位）：2020-09-11
        LocalDate anotherDay = otherDay.withYear(2020).withMonth(9).withDayOfMonth(11);

        //获取2020年最后一天：2020-12-31
        LocalDate lastDay = anotherDay.withDayOfYear(366);

        //判断2020年是否为闰年：true
        boolean isLeapYear = lastDay.isLeapYear();

        //获取birthday是哪一年：1995
        int weekBasedYear = birthday.get(IsoFields.WEEK_BASED_YEAR);

        //获取birthday是该年的第几周：21
        int weekOfWeekBasedYear = birthday.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR);

        //获取birthday是该年的第几季度：2
        int quarterOfYear = birthday.get(IsoFields.QUARTER_OF_YEAR);

        //获取birthday是该季度的第几天：53
        int daysOfQuarter = birthday.get(IsoFields.DAY_OF_QUARTER);

        //定义日期格式：****年**月**日
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy" + "年" + "MM" + "月" + "dd" + "日");

        String birthdayFormat = birthday.format(dateFormatter);

        String todayFormat = today.withDayOfMonth(1).format(dateFormatter);

        System.out.println(birthdayFormat);

        System.out.println(todayFormat);

        Period age = birthday.until(today);

        //介绍 IsoFields 和 ChronoField
        int quarterOfYear1 = birthday.get(ChronoField.DAY_OF_YEAR);

        LocalDate startDate = LocalDate.of(2021, 10, 30);
        LocalDate endDate = LocalDate.of(2021, 10, 30);
        int dayToHours = Period.between(startDate, endDate).getDays();
        int hours = LocalTime.now().getHour() * 8 / 24;
        int number = (dayToHours - 1) * 8 + hours;
        if (number < 0) {
            number = 0;
        }
        System.out.println(number);

    }

}
